package cn.autumnorange;

import cn.autumnorange.commons.util.TokenUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
//@EnableResourceServer
public class ChanelApplication
//        extends ResourceServerConfigurerAdapter

{

    @Value("${server.port}")
    String port;

    @Value("${local-server}")
    String localServer;
    @Value("${security.oauth2.client.access-token-uri}")
    String accessTokenUri;

    public static void main(String[] args) {
        SpringApplication.run( ChanelApplication.class, args );
    }

    @RequestMapping("/hi")
    public OAuth2AccessToken getToken(String code, String state) {
        return TokenUtil.getToken(accessTokenUri, "Basic b3JkZXI6b3JkZXI=", code, localServer + "/hi", state);
    }

    @PreAuthorize("hasRole('ROLE_admin')")
    @RequestMapping("/test1")
    public String test(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi test11111111 " + name + " ,i am from port:" + port;
    }


}
